package com.cljz.peipei.present.activity;

import com.cljz.peipei.base.BaseP;
import com.cljz.peipei.model.net.api.Api;
import com.cljz.peipei.model.net.bean.GetUserInfoBean;
import com.cljz.peipei.model.net.bean.ResponseBean;
import com.cljz.peipei.ui.activity.VideoCallActivity;
import com.clzj.peipei.bean.Tag;

import java.util.ArrayList;
import java.util.List;

import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;

/**
 * Created by apple on 2017/5/9.
 */

public class VideoCallP extends BaseP<VideoCallActivity>{

    public void getUserInfoData(String otherUserId) {
        Api.getResponseInfoAPI().getUserInfoData(getToken(), otherUserId)
                .compose(XApi.<GetUserInfoBean>getApiTransformer())
                .compose(XApi.<GetUserInfoBean>getScheduler())
                .compose(getV().<GetUserInfoBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<GetUserInfoBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(GetUserInfoBean getUserInfoBean) {
                        if ("0".equals(getUserInfoBean.getCode())) {
                            getV().getUserInfoSucceed(getUserInfoBean);
                        } else if ("1000".equals(getUserInfoBean.getCode())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().getUserInfoFail(getUserInfoBean.getMsg());
                        }
                    }
                });
    }

    public void getUserInfoData2(String otherUserId) {
        Api.getResponseInfoAPI().getUserInfoData(getToken(), otherUserId)
                .compose(XApi.<GetUserInfoBean>getApiTransformer())
                .compose(XApi.<GetUserInfoBean>getScheduler())
                .compose(getV().<GetUserInfoBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<GetUserInfoBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(GetUserInfoBean getUserInfoBean) {
                        if ("0".equals(getUserInfoBean.getCode())) {
                            getV().getUserInfoSucceed2(getUserInfoBean);
                        } else if ("1000".equals(getUserInfoBean.getCode())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().getUserInfoFail(getUserInfoBean.getMsg());
                        }
                    }
                });
    }

    public List<String> getTags(){
        List<Tag> tags = daoSession.getTagDao().loadAll();
        List<String > list = new ArrayList<>();
        for (Tag t : tags) {
            list.add(t.getTag_name());
        }

        return list;
    }

    public void callEnd() {
        Api.getResponseInfoAPI().callEndData(getToken())
                .compose(XApi.<ResponseBean>getApiTransformer())
                .compose(XApi.<ResponseBean>getScheduler())
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {

                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if ("0".equals(responseBean.getCode())) {
                            getV().callEndSucceed(responseBean);
                        } else if ("1000".equals(responseBean.getCode())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().callEndFail(responseBean.getMsg());
                        }
                    }
                });
    }

    public void callSuccess() {
        Api.getResponseInfoAPI().callSuccessData(getToken())
                .compose(XApi.<ResponseBean>getApiTransformer())
                .compose(XApi.<ResponseBean>getScheduler())
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {

                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if ("0".equals(responseBean.getCode())) {
                            getV().callSucceed(responseBean);
                        } else if ("1000".equals(responseBean.getCode())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().callFail(responseBean.getMsg());
                        }
                    }
                });
    }
}
